package com.winit.requirement.web;

import com.winit.common.base.web.BaseController;
import com.winit.requirement.manager.ReqDebunkManager;
import com.winit.requirement.model.ReqDebunker;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * req宣讲吐槽记录
 */
@Api(value = "debunk", tags = {"debunk"}, description = "宣讲记录")
@RestController
@RequestMapping("/api/v1/debunk")
public class ReqDebunkController extends BaseController {

    private static Logger log = LoggerFactory.getLogger(ReqDebunkController.class);

    @Autowired
    ReqDebunkManager manager;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "保存记录，包括新建和更新")
    @ResponseBody
    public Map save(@RequestBody ReqDebunker debunker) {
        if (null == debunker) {
            log.error("没有接收到参数.");
            return this.getRtn(BaseController.REQUEST_ERROR, "没有接收到参数.", null);
        }
        ReqDebunker rs = null;
        if (StringUtils.isEmpty(debunker.getId())) {
            log.info("没有id，将执行新增操作");
            rs = this.manager.create(debunker);
        } else {
            log.info("执行更新操作");
            rs = this.manager.update(debunker);
        }
        return this.getRtn(BaseController.REQUEST_SUCCESS, BaseController.REQUEST_SUCCESS_MSG, rs);
    }

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "查询宣讲记录")
    @ResponseBody
    public Map get(@ApiParam(value = "id，记录id") @RequestParam(value = "id", required = false) String id,
                   @ApiParam(value = "reqId", required = true) @RequestParam(value = "reqId") String reqId) {
        List<ReqDebunker> list = manager.get(id, reqId, null);
        return this.getRtn(BaseController.REQUEST_SUCCESS, BaseController.REQUEST_SUCCESS_MSG, list);
    }
}
